//The MIT License

//Copyright (c) 2009 Lluis Sanchez, Nick Darnell

//Permission is hereby granted, free of charge, to any person obtaining a copy
//of this software and associated documentation files (the "Software"), to deal
//in the Software without restriction, including without limitation the rights
//to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//copies of the Software, and to permit persons to whom the Software is
//furnished to do so, subject to the following conditions:

//The above copyright notice and this permission notice shall be included in
//all copies or substantial portions of the Software.

//THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
//THE SOFTWARE.

using System;
using System.Collections;
using System.Reflection;
using System.Reflection.Emit;
using System.Collections.Generic;
using System.Collections.ObjectModel;

namespace Polymorph
{
    /// <summary>
    /// The <see cref="CodeBlock"/> class represents a container scope for 
    /// code items.
    /// </summary>
    public class CodeBlock : CodeItem
    {
        private List<CodeItem> m_statements;

        /// <summary>
        /// Initializes a new instance of <see cref="CodeBlock"/>.
        /// </summary>
        public CodeBlock()
        {
            m_statements = new List<CodeItem>();
        }

        /// <summary>
        /// Gets all the items in a code block.
        /// </summary>
        public ReadOnlyCollection<CodeItem> Items
        {
            get { return m_statements.AsReadOnly(); }
        }

        /// <summary>
        /// Adds a new code item to the <see cref="CodeBlock"/>.
        /// </summary>
        /// <param name="code">The code item to add to the code block.</param>
        public void Add(CodeItem code)
        {
            m_statements.Add(code);
        }

        /// <summary>
        /// Gets a value indicating if the code block is empty.
        /// </summary>
        public bool IsEmpty
        {
            get { return m_statements.Count == 0; }
        }

        public CodeItem GetLastItem()
        {
            //if (IsEmpty)
            //    return null;

            return m_statements[m_statements.Count - 1];
        }

        /// <summary>
        /// Adds a code expression to the code block.
        /// </summary>
        /// <param name="cb">The code block to append the expression to.</param>
        /// <param name="e">The expression to append.</param>
        /// <returns>The codeblock the expression was added to.</returns>
        public static CodeBlock operator +(CodeBlock cb, CodeExpression e)
        {
            cb.Add(e);
            return cb;
        }

        /// <summary>
        /// Generates the IL for the <see cref="CodeBlock"/> and all the sub code items 
        /// contained in the block.
        /// </summary>
        /// <param name="gen">The IL generator to use.</param>
        public override void Generate(ILGenerator gen)
        {
            foreach (CodeItem item in m_statements)
            {
                if (item is CodeExpression)
                    ((CodeExpression)item).GenerateAsStatement(gen);
                else
                    item.Generate(gen);
            }
        }

        /// <summary>
        /// Prints the code block in a human readable format.
        /// </summary>
        /// <param name="cw">The code writer to use.</param>
        public override void PrintCode(CodeWriter cw)
        {
            foreach (CodeItem item in m_statements)
            {
                cw.BeginLine();
                item.PrintCode(cw);
                //if (item is CodeExpression)
                    cw.Write(";");
                cw.EndLine();
            }
        }
    }
}